package lianxi02;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2024-04-21
 * Time: 13:09
 */
public class MyThread extends Thread{
    static int gift =100;

    //锁对象
    static Object obj = new Object();

    @Override
    public void run() {
        while(true) {
            synchronized (obj) {
                if(gift > 10) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    gift--;
                    System.out.println(getName()+"的礼物还有"+gift+"份!!");
                }else {
                    System.out.println("礼物小于10份,不再送出!");
                    break;
                }
            }
        }
    }
}
